﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;
namespace BusinessLogic
{
  public  static class CongTy
    {
        public static Entities.CongTy[] Select(Entities.CongTy nv)
        {
            Entities.CongTy[] list = null;
            try
            {
                //lay mo ket noi voi' chuoi~ ket noi lay' tu` file app.confg trong GUI
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.CongTy.SelectCongTy, cn);
                cmd.Parameters.Add(Common.Constants.CongTy.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                string directory = System.Windows.Forms.Application.StartupPath.ToString() + @"\CongTyImage";
                Common.Utilities.CreateDirectory(directory);
                while (dr.Read())
                {
                    Entities.CongTy row = new Entities.CongTy();
                    row.CongTyID = int.Parse(dr[Common.Constants.CongTy.CongTyID].ToString());
                    row.MaCongTy = dr[Common.Constants.CongTy.MaCongTy].ToString();
                    row.TenCongTy = dr[Common.Constants.CongTy.TenCongTy].ToString();
                    row.DiaChi = dr[Common.Constants.CongTy.DiaChi].ToString();
                    row.Email = dr[Common.Constants.CongTy.Email].ToString();
                    row.Website = dr[Common.Constants.CongTy.Website].ToString();
                    row.DienThoai = dr[Common.Constants.CongTy.DienThoai].ToString();
                    row.Fax = dr[Common.Constants.CongTy.Fax].ToString();
                    string links = System.Windows.Forms.Application.StartupPath.ToString() + @"\CongTyImage\" + dr[Common.Constants.CongTy.LinkImage].ToString();
                    row.LinkImage = dr[Common.Constants.CongTy.LinkImage].ToString();
                    row.LinkImageRoot = links;
                    row.Images = null;
                    row.GhiChu = dr[Common.Constants.CongTy.GhiChu].ToString();
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.CongTy[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.CongTy)arr[i];
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }

        // Selct nhà cung cấp
        public static Entities.CongTy Select1(Entities.CongTy congty1)
        {
            Entities.CongTy congty = new Entities.CongTy();
            try
            {
                string sql = Common.Constants.CongTy.SelectTheoMaCongTy;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.CongTy.MaCongTy, SqlDbType.NVarChar, 50).Value = congty1.MaCongTy;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    congty.CongTyID = int.Parse(dr[Common.Constants.CongTy.CongTyID].ToString());
                    congty.MaCongTy = dr[Common.Constants.CongTy.MaCongTy].ToString();
                    congty.TenCongTy = dr[Common.Constants.CongTy.TenCongTy].ToString();
                    congty.DiaChi = dr[Common.Constants.CongTy.DiaChi].ToString();
                    congty.Email = dr[Common.Constants.CongTy.Email].ToString();
                    congty.Website = dr[Common.Constants.CongTy.Website].ToString();
                    congty.DienThoai = dr[Common.Constants.CongTy.DienThoai].ToString();
                    congty.Fax = dr[Common.Constants.CongTy.Fax].ToString();
                    congty.GhiChu = dr[Common.Constants.CongTy.GhiChu].ToString();
                    if (dr[Common.Constants.CongTy.LinkImage].ToString() != "")
                    {
                        try
                        {
                            congty.LinkImage = System.Windows.Forms.Application.StartupPath.ToString() + @"\CongTyImage\" + dr[Common.Constants.CongTy.LinkImage].ToString() + ".jpg";
                            congty.Image = XuLyAnh(congty.LinkImage);
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        congty.LinkImage = null;
                        congty.Image = null;
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
                
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.CongTy(); }
            return congty;
        }
        // xử lý ảnh
        private static byte[] XuLyAnh(string links)
        {
            byte[] image = null;
            try
            {
                Bitmap bmp = new Bitmap(links);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] manganh = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(manganh, 0, Convert.ToInt32(ms.Length));
                image = manganh;

                ms.Dispose();
                ms.Close();
                ms.Flush();
                bmp.Dispose();
                bmp.Clone();
            }
            catch (Exception ex)
            { string s = ex.Message; }
            return image;
        }
        // insert update
        public static int InsertUpdate(Entities.CongTy nhom)
        {
            int congty = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.CongTy.InsertUpdateCongTy, cn);
                cmd.Parameters.Add(Common.Constants.CongTy.HanhDong, SqlDbType.NVarChar).Value = nhom.HanhDong;
                cmd.Parameters.Add(Common.Constants.CongTy.CongTyID, SqlDbType.Int).Value = nhom.CongTyID;
                cmd.Parameters.Add(Common.Constants.CongTy.MaCongTy, SqlDbType.VarChar, 50).Value = nhom.MaCongTy;
                cmd.Parameters.Add(Common.Constants.CongTy.TenCongTy, SqlDbType.NVarChar, 200).Value = nhom.TenCongTy;
                cmd.Parameters.Add(Common.Constants.CongTy.DiaChi, SqlDbType.NVarChar, 200).Value = nhom.DiaChi;
                cmd.Parameters.Add(Common.Constants.CongTy.Email, SqlDbType.NVarChar, 50).Value = nhom.Email;
                cmd.Parameters.Add(Common.Constants.CongTy.Website, SqlDbType.NVarChar, 200).Value = nhom.Website;
                cmd.Parameters.Add(Common.Constants.CongTy.DienThoai, SqlDbType.NVarChar, 20).Value = nhom.DienThoai;
                cmd.Parameters.Add(Common.Constants.CongTy.Fax, SqlDbType.NVarChar, 20).Value = nhom.Fax;
                string linked = string.Empty;
                if (nhom.LinkImage != string.Empty)
                {
                    linked = nhom.MaCongTy + ".jpeg";
                }
                else
                {
                    linked = string.Empty;
                }
                cmd.Parameters.Add(Common.Constants.CongTy.LinkImage, SqlDbType.NVarChar).Value = linked;
                cmd.Parameters.Add(Common.Constants.CongTy.GhiChu, SqlDbType.NVarChar, 200).Value = nhom.GhiChu;

                congty = cmd.ExecuteNonQuery();
                if (congty != 0)
                {
                    if (linked != string.Empty)
                    {
                        string links = @"\CongTyImage\" + linked;
                        string linksRoot = System.Windows.Forms.Application.StartupPath.ToString() + links;
                        try { Common.Utilities.DeleteFile(links); }
                        catch { }
                        System.IO.MemoryStream ms = new System.IO.MemoryStream(nhom.Images);
                        Bitmap bmp = new Bitmap(ms);
                        try
                        {
                            bmp.Save(linksRoot, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        catch { }
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                congty = 0;
            }
            return congty;

        }
        public static byte[] GetImage(string linksRoot)
        {
            byte[] image = null;
            try
            {
                image = Common.Utilities.GetImage(linksRoot);
            }
            catch (Exception ex)
            { string s = ex.Message; image = null; }
            return image;
        }

    }
}
